#!/bin/bash

#parm1 filename
#parm2 block name
#parm3 key
#return value
#desc get value with key form ini file
function get_field_value()
{
	if [ ! -f $1 ] || [ $# -ne 3 ];then
		return 1
	fi
	blockname=$2
	fieldname=$3
	begin_block=0
	end_block=0
	while read line || [[ -n ${line} ]]
	do
		if [ "X$line" = "X[$blockname]" ];then
			begin_block=1
			continue
		fi
		if [ $begin_block -eq 1 ];then
			end_block=$(echo $line | awk 'BEGIN{ret=0} /^\[.*\]$/{ret=1} END{print ret}')
			if [ $end_block -eq 1 ];then
				#echo "end block"
				break
			fi
			need_ignore=$(echo $line | awk 'BEGIN{ret=0} /^#/{ret=1} /^$/{ret=1} END{print ret}')
			if [ $need_ignore -eq 1 ];then
				#echo "ignored line:" $line
				continue
			fi
			field=$(echo $line | awk -F= '{gsub(" |\t","",$1); print $1}')
			value=$(echo $line | awk 'gsub(/^ *| *$/,"")' | awk -F= '{gsub("\t","",$2); print $2}')
			#echo "'$field':'$value'"
			if [ "X$fieldname" = "X$field" ];then
				#echo "result value:'$result'"
				echo ${value// /}
				break
			fi
		fi
	done < $1
	return 0
}

#parm1 filename
#parm2 block name
#parm3 key
#parm4 value
#desc set value with key form ini file
function set_field_value()
{
	i=0
	has_filed=0
	has_block=0
	if [ ! -f $1 ] || [ $# -ne 4 ];then
		return 1
	fi
	blockname=$2
	fieldname=$3
	value=$4
	begin_block=0
	end_block=0
	while read line || [[ -n ${line} ]]
	do
        ((i+=1))
		if [ "X$line" = "X[$blockname]" ];then
			begin_block=1
			has_block=1
			continue
		fi
		if [ $begin_block -eq 1 ];then
			end_block=$(echo $line | awk 'BEGIN{ret=0} /^\[.*\]$/{ret=1} END{print ret}')
			if [ $end_block -eq 1 ];then
				break
			fi
			need_ignore=$(echo $line | awk 'BEGIN{ret=0} /^#/{ret=1} /^$/{ret=1} END{print ret}')
			if [ $need_ignore -eq 1 ];then
				continue
			fi
			field=$(echo $line | awk -F= '{gsub(" |\t","",$1); print $1}')
			if [ "X$fieldname" = "X$field" ];then
				sed -i "${i}c\
				${field}=${value}" $1
				has_filed=1
				break
			fi
		fi
	done < $1
	if [ $has_filed -eq 0 ];then
		sed -i  "/\[$blockname\]/a\\${fieldname}=${value}" $1
	fi
	if [ $has_block -eq 0 ];then
	    sed -i "$ a  \[$blockname\]" $1
		sed -i  "/\[$blockname\]/a\\${fieldname}=${value}" $1
	fi
	return 0
}


function clear_env()
{
	sudo docker stop $1
	sudo docker rm $1
	rm -rf $2
}

master_port=${1}
slave_port=${2}
mysql_pwd=${3:-'123456'}
remove_folder=${4:-'false'}  # 是否清空环境


#模板物理全路径
mysql_template_path="/opt/docker_work/mysql_template"

master_name="mysql_"${1}
master_path="/opt/docker_work/"${master_name}
slave_name="mysql_"${2}
slave_path="/opt/docker_work/"${slave_name}

port=$(get_field_value $mysql_template_path/master/conf/my.cnf mysqld server-id)

if  [[ "$remove_folder" == 'true' ]];then
    echo "remove_folder............."
    clear_env $master_name $master_path
		clear_env $slave_name $slave_path
fi


cp -r $mysql_template_path/master  $master_path
cp -r $mysql_template_path/slave   $slave_path
#set_field_value $upu_conf info plat_base_ip $plat_base_ip
#ifconfig eth0 | grep "inet " | cut -d " " -f 10
set_field_value $master_path/conf/my.cnf mysqld server-id $master_port
set_field_value $slave_path/conf/my.cnf mysqld server-id $slave_port

sudo docker run --name $master_name  -p $master_port:3306 -v $master_path/data:/var/lib/mysql -v $master_path/logs:/logs  -v $master_path/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=$mysql_pwd -d  mysql:5.7
sudo docker run --name $slave_name  -p $slave_port:3306 -v $slave_path/data:/var/lib/mysql -v $slave_path/logs:/logs  -v $slave_path/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=$mysql_pwd -d  mysql:5.7

echo "sleep 10s"
sleep 10
sudo docker ps
sudo docker exec -it $master_name /bin/bash -c "service mysql restart"
sudo docker exec -it $slave_name /bin/bash -c "service mysql restart"
sudo docker start $master_name
sudo docker start $slave_name

#主库
sudo docker exec -it $master_name /bin/bash -c "service mysql status"
sudo docker exec -it $master_name /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p${mysql_pwd} -e \"CREATE USER 'slave'@'%' IDENTIFIED BY '123456';GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';flush privileges;\""
#sudo docker exec -it mysql_3310 /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p123456 -e \"CREATE USER 'slave3'@'%' IDENTIFIED BY '123456';GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave3'@'%';flush privileges;\""
#sudo docker exec -it mysql_3311 /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p123456 -e \"change master to master_host='172.17.0.4', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000003', master_log_pos=769, master_connect_retry=30;\""
master_log_file=$(sudo docker exec -it $master_name /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p${mysql_pwd} -e \"show master status;\"" | grep mysql-bin | cut -d \| -f 2 | sed 's/\n//g' |sed 's/ //g')
master_log_pos=$(sudo docker exec -it $master_name /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p${mysql_pwd} -e \"show master status;\"" | grep mysql-bin | cut -d \| -f 3  | sed 's/\n//g' |sed 's/ //g')
echo $master_log_file$master_log_pos
#sudo docker exec -it $master_name /bin/bash -c "service mysql restart"

#获取主库的分配IP
master_host=$(sudo docker inspect $master_name --format='{{.NetworkSettings.IPAddress}}')
echo "change master to master_host=${master_host}, master_user='slave', master_password=${mysql_pwd}, master_port=3306, master_log_file=${master_log_file}, master_log_pos=${master_log_pos}, master_connect_retry=30;"
sudo docker exec -it $slave_name /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p${mysql_pwd} -e \"change master to master_host='${master_host}', master_user='slave', master_password='${mysql_pwd}', master_port=3306, master_log_file='${master_log_file}', master_log_pos=${master_log_pos}, master_connect_retry=30;\""
sudo docker exec -it $slave_name /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p${mysql_pwd} -e \"start slave;\""

echo "-------------从库slave状态：--------------"
sudo docker exec -it $slave_name /bin/bash -c "mysql -h127.0.0.1 -P3306 -uroot -p${mysql_pwd} -e \"show slave status;\""


